﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orchard.ContentManagement.Handlers;
using Orchard;
using Downplay.Origami.Drivers;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;

namespace Downplay.Prototypes.Lens.Handlers {

    /// <summary>
    /// Decorates Content shapes with data needed for Lens to function
    /// </summary>
    public class LensDataContentHandler : ContentHandler {

        public LensDataContentHandler(
            IOrchardServices orchardServices
            ) {
                Services = orchardServices;
        }

        public IOrchardServices Services { get; set; }

        protected override void BuildDisplayShape(BuildDisplayContext context) {

            if (context.Shape.ContentItem != null) {
                ContentItem content = context.Shape.ContentItem;
                if (content!=null) {
                    context.Shape.Attributes["data-content-item-id"]=content.Id.ToString();
                    if (content.VersionRecord != null) {
                        context.Shape.Attributes["data-content-item-version-id"] = content.VersionRecord.Id.ToString();
                    }
                }
            }
            // TODO: Adding these attributes onto *all* items could be seen as a bad thing. But realistically there are tons of situations where it'll be useful. Anyway,
            // Orchard already surfaces Ids in other places, might as well make it official.
            //            var isAdmin = Services.Value.Authorizer.Authorize(Orchard.Core.Contents.Permissions.EditContent, Model.ContentConnector);
            // TODO: Also, we shouldn't be doing this from Lens, maybe Mechanics?
        }

    }
}
